package com.greet.controller.admin;

import com.greet.constant.JwtClaimsConstant;
import com.greet.dto.EmployeeDTO;
import com.greet.dto.EmployeeLoginDTO;
import com.greet.dto.EmployeePageQueryDTO;
import com.greet.dto.PasswordEditDTO;
import com.greet.entity.Employee;
import com.greet.properties.JwtProperties;
import com.greet.result.PageResult;
import com.greet.result.Result;
import com.greet.service.EmployeeService;
import com.greet.utils.JwtUtil;
import com.greet.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")

@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    @PostMapping
    public Result<Object> addNewEmployee(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工: {}", employeeDTO);
        boolean resInfo = employeeService.addNewEmployee(employeeDTO);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<PageResult> getPageQuery(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询");
        PageResult pageResult = employeeService.getPageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }


    @PostMapping("/status/{status}")
    public Result setStatus(@PathVariable Integer status, Long id){
        log.info("设置账号启用状态 id {}, 状态码{}", id, status);
        employeeService.setStatus(status, id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Employee> selectById(@PathVariable Long id){
        Employee res = employeeService.selectById(id);
        return Result.success(res);
    }

    @PutMapping
    public Result updateEmp(@RequestBody EmployeeDTO employeeDTO){
        log.info("修改员工数据{}", employeeDTO);
        employeeService.updateEmp(employeeDTO);
        return Result.success();
    }

    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO){
        employeeService.editPassword(passwordEditDTO);
        return Result.success();
    }


}
